﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Chbl.Models;

namespace Chbl.DAL
{
    public class ChblDbInitializer : DropCreateDatabaseIfModelChanges<ChblDb>
    {
        protected override void Seed(ChblDb context)
        {
            base.Seed(context);
            var seasons = new List<Season>
                                  {
                                      new Season
                                          {
                                              Begin = DateTime.Parse("21.11.2009"),
                                              End = DateTime.Parse("27.03.2010"),
                                              Number = 2
                                          },
                                      new Season
                                          {
                                              Begin = DateTime.Parse("20.11.2010"),
                                              End = DateTime.Parse("26.03.2011"),
                                              Number = 3
                                          }
                                  };
            var divisions = new List<Division>
                                    {
                                        new Division{Name = "А"},
                                        new Division{Name = "Б"}
                                    };
            var teamsA = new List<Team>
                                 {
                                     new Team {Name = "Хижаки"},
                                     new Team {Name = "Бродяги"},
                                     new Team {Name = "Бульдоги"},
                                     new Team {Name = "Каштан"}
                                 };

            var teamsB = new List<Team>
                                 {
                                     new Team{ Name = "Богомолы"},
                                     new Team {Name = "Гравити"},
                                     new Team{ Name = "Флеш"},
                                     new Team {Name = "Грин Тим"}
                                 };
            var seasonDivisions = new List<SeasonDivision>
                                          {
                                              new SeasonDivision
                                                  {
                                                      Division = divisions[0],
                                                      Season = seasons[1],
                                                      Teams = teamsA
                                                  },
                                              new SeasonDivision
                                                  {
                                                      Division = divisions[1],
                                                      Season = seasons[1],
                                                      Teams = teamsB
                                                  }
                                          };

            var members = new List<Member>
                                  {
                                      new Member
                                          {
                                              BirthDate = new DateTime(1984, 6, 11),
                                              Fio = "Сафронов Евгений Анатольевич"

                                          },
                                      new Member
                                          {
                                              BirthDate = new DateTime(1987, 12, 2),
                                              Fio = "Иванов Иван Иванович"
                                          }
                                  };
            var players = new List<Player>
                                  {
                                      new Player
                                          {
                                              Member = members[0],
                                              Height = 185
                                          },
                                      new Player
                                          {
                                              Member = members[1],
                                              Height = 193
                                          }
                                  };
            var teamPlayers = new List<TeamPlayer>
                                      {
                                          new TeamPlayer
                                              {
                                                  BeginDate = new DateTime(2010, 1, 1),
                                                  EndDate = DateTime.MaxValue,
                                                  Player = players[0],
                                                  Team = teamsB[1]
                                              },
                                          new TeamPlayer
                                              {
                                                  BeginDate = new DateTime(2008, 8, 21),
                                                  EndDate = DateTime.MaxValue,
                                                  Player = players[1],
                                                  Team = teamsA[0]
                                              }
                                      };
            members.ForEach(m => context.Members.Add(m));
            players.ForEach(p => context.Players.Add(p));
            teamPlayers.ForEach(tp => context.TeamPlayers.Add(tp));
            seasons.ForEach(s => context.Seasons.Add(s));
            divisions.ForEach(d => context.Divisions.Add(d));
            teamsA.ForEach(t => context.Teams.Add(t));
            teamsB.ForEach(t => context.Teams.Add(t));
            seasonDivisions.ForEach(sd => context.SeasonDivisions.Add(sd));
            context.SaveChanges();
        }
    }
}